/**
 * @author: ares
 * @date: 2020/2/1 12:10
 * @description: menu操作
 */
layui.define(["element", "jquery", "commonServices"], function (exports) {
	var element = layui.element,
		$ = layui.$,
		commonServices = layui.commonServices;

	function Menu() {
	}

	Menu.prototype = {
		constructor: Menu,

		/**
		 * @author: ares
		 * @date: 2020/1/29 17:00
		 * @description: 获取侧边菜单
		 */
		getSideMenu: function () {
			var that = this;
			return commonServices.getSideMenuService().then(function (res) {
				that.renderSideMenu(res.data)
				return res.data;
			})
		},

		/**
		 * @author: ares
		 * @date: 2020/1/29 17:01
		 * @description: 渲染侧边菜单
		 * @param {array}list
		 */
		renderSideMenu: function (list) {
			var $sideMenuFragment = $(document.createDocumentFragment()),
				$sideTreeMenu = $("#side-tree-menu");
			$.each(list, function (index, menu) {
				var linkUrl = menu.href || '',
					$menuItemLi = $('<li class="layui-nav-item">' +
						'<a href="#' + linkUrl + '">' +
						'<i class="' + menu.icon + '"></i>' +
						'<cite>' + menu.title + '</cite>' +
						'</a>' +
						'</li>');
				if(!linkUrl) $menuItemLi.find('a').attr('href','javascript:;')
				//是否添加target属性
				if (menu.target && typeof menu.target == 'string') $menuItemLi.find('a').attr('target', menu.target);
				//是否有子菜单
				if (($.isArray(menu.children) && menu.children.length) || ($.isArray(menu.child) && menu.child.length)) {
					//构建子菜单
					buildChildrenMenu($menuItemLi, menu.child || menu.children)
				}
				$sideMenuFragment.append($menuItemLi)
			})
			$sideTreeMenu.empty().append($sideMenuFragment)
			element.render()
		},
	};

	/**
	 * @author: ares
	 * @date: 2020/2/3 14:29
	 * @description: 生成子菜单
	 * @param {object}jqElement 父菜单jqDom
	 * @param {array}list  数据
	 * @param {number}defaultPaddingLeft  默认左偏移
	 */
	function buildChildrenMenu(jqElement, list, defaultPaddingLeft) {
		jqElement.children('a').append('<span class="layui-nav-more"></span>');
		//获取父菜单的缩进量
		var parentPaddingLeft = jqElement.children('a').css('padding-left'),
			defaultPaddingLeft = typeof defaultPaddingLeft == 'number' ? defaultPaddingLeft : 20,
			childrenPaddingLeft = parentPaddingLeft ?
				1 * parentPaddingLeft.replace('px', '') + 10 : defaultPaddingLeft + 10,
			childrenDl = $('<dl class="layui-nav-child"></dl>');
		$.each(list, function (index, menu) {
			var linkUrl = menu.href || '',
				childrenDd = $('<dd>' +
					'<a href="#' + linkUrl + '">' +
					'<i class="' + menu.icon + '"></i>' +
					'<cite>' + menu.title + '</cite>' +
					'</a>' +
					'</dd>');
			if(!linkUrl) childrenDd.find('a').attr('href','javascript:;')
			//相对上级菜单缩进
			childrenDd.children('a').css('padding-left', childrenPaddingLeft)
			//是否添加target属性
			if (menu.target && typeof menu.target == 'string') childrenDd.find('a').attr('target', menu.target);
			//是否有子菜单
			if (($.isArray(menu.children) && menu.children.length) || ($.isArray(menu.child) && menu.child.length)) {
				buildChildrenMenu(childrenDd, menu.child || menu.children)
			}
			childrenDl.append(childrenDd)
		})
		jqElement.append(childrenDl)
	}

	exports("menu", new Menu());
})
